package com.fengye.serialization.serializer.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fengye.serialization.serializer.Serializer;

/**
 * @Author fengye
 * @Date 2023/2/13 9:25
 **/
public class FASTJSONSerializer implements Serializer {
    @Override
    public <T> byte[] serialize(T obj) throws Exception {
        if(obj == null) {
            return new byte[0];
        }

        return JSON.toJSONBytes(obj, SerializerFeature.WriteDateUseDateFormat);
    }

    @Override
    public <T> T deserialize(byte[] data, Class<T> clazz) throws Exception {
        if(data == null) {
            return null;
        }
        return JSON.parseObject(new String(data), clazz);
    }
}
